
package control.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.CharacterBEAN;
import bean.EquipmentBEAN;
import dao.CharacterDAO;
import dao.EquipmentDAO;

/**
 * Servlet implementation class Navigation
 */
public class Admin extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private CharacterDAO		characterDAO;
	private EquipmentDAO		equipmentDAO;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Admin() {
		super();
	}

	public void init( ServletConfig config ) throws ServletException {
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
		equipmentDAO = (EquipmentDAO) config.getServletContext().getAttribute( "EquipmentDAO" );
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		System.out.println( "dsa" );
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			request.setAttribute( "character", characterBEAN );
			if( action.equals( "searchUser" ) ) {
				String userName = (String) request.getParameter( "userName" );
				Object userId = request.getParameter( "userId" );
				if( userName == null && userId == null ) {
					goTo( "WEB-INF/logged/admin/managementOverview.jsp",
							request, response );
					return;
				} else if( userName != null ) {
					int charId = characterDAO
							.getCharIDByName( userName );
					CharacterBEAN foundUser = characterDAO
							.getCharacterByID( charId );
					EquipmentBEAN foundEquipment = equipmentDAO.getEquipmentByID( charId );
					request.setAttribute( "foundUser", foundUser );
					request.setAttribute( "foundEquipment", foundEquipment );
					goTo( "WEB-INF/logged/admin/characterManagement.jsp",
							request, response );
					return;
				} else if( userId != null ) {
					CharacterBEAN foundUser = characterDAO
							.getCharacterByID( Integer.parseInt( userId
									.toString() ) );
					request.setAttribute( "foundUser", foundUser );
					goTo( "WEB-INF/logged/admin/characterManagement.jsp",
							request, response );
					return;
				}
			} else if( action.equals( "editUser" ) ) {
				int userId = Integer.parseInt( request.getParameter( "id" )
						.toString() );
				int exp = Integer.parseInt( request.getParameter( "experience" )
						.toString() );
				int moneyCash = Integer.parseInt( request.getParameter(
						"moneyCash" ).toString() );
				int moneyBank = Integer.parseInt( request.getParameter(
						"moneyBank" ).toString() );
				int shootExperience = Integer.parseInt( request.getParameter(
						"shootExperience" ).toString() );
				int respect = Integer.parseInt( request
						.getParameter( "respect" ).toString() );
				boolean vip = false;
				if( request.getParameter( "vip" ).equals( "true" ) ) {
					vip = true;
				};
				boolean admin = false;
				if( request.getParameter( "admin" ).equals( "true" ) ) {
					admin = true;
				};
				boolean sniper = false;
				if( request.getParameter( "sniper" ).equals( "true" ) ) {
					sniper = true;
				};
				boolean uziSMG = false;
				if( request.getParameter( "uziSMG" ).equals( "true" ) ) {
					uziSMG = true;
				};
				boolean desertEagle = false;
				if( request.getParameter( "desertEagle" ).equals( "true" ) ) {
					desertEagle = true;
				};
				boolean barakSP21 = false;
				if( request.getParameter( "barakSP21" ).equals( "true" ) ) {
					barakSP21 = true;
				};
				boolean coltM1911 = false;
				if( request.getParameter( "coltM1911" ).equals( "true" ) ) {
					coltM1911 = true;
				};
				boolean knife = false;
				if( request.getParameter( "knife" ).equals( "true" ) ) {
					knife = true;
				};
				boolean bulletFreeArmor = false;
				if( request.getParameter( "bulletFreeArmor" ).equals( "true" ) ) {
					bulletFreeArmor = true;
				};
				boolean bulletFreeHelmet = false;
				if( request.getParameter( "bulletFreeHelmet" ).equals( "true" ) ) {
					bulletFreeHelmet = true;
				};
				boolean bombArmor = false;
				if( request.getParameter( "bombArmor" ).equals( "true" ) ) {
					bombArmor = true;
				};
				int bodyguards = Integer.parseInt( request.getParameter(
						"bodyguard" ).toString() );
				int bullets = Integer.parseInt( request.getParameter(
						"bullet" ).toString() );
				equipmentDAO.updateEquipment( new EquipmentBEAN( userId, sniper, false, uziSMG, desertEagle, barakSP21, coltM1911, knife, bulletFreeArmor, bulletFreeHelmet, bombArmor, bodyguards, bullets ) );
				characterDAO.updateCharacter( userId, exp, moneyCash,
						moneyBank, shootExperience, respect, vip, admin );
				goTo( "WEB-INF/logged/admin/managementOverview.jsp", request,
						response );
				return;
			}
		} else {
		}
	}

	private void goTo( String action, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		try {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher( action );
			dispatcher.forward( request, response );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}
}
